d<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    
	
	<title>_分仓管理</title>
</head>

<body>
<section class="content">
<script>
	var dataTables_areagroup;
	
	$(function(){
		initTable();
	});
	
	//查询
	function query(){
		if($("#queryForm").valid()){
			if (dataTables_areagroup) {
				dataTables_areagroup.fnDestroy();
			}
			initTable();
		}
	}
	
	//分页查询结果
	function initTable(){
		//初始化dataTables-快递单流量信息
	    dataTables_areagroup=$("#contentTable").dataTable({
	       "bServerSide":true,
		   "sScrollX": "100%",
		   "sScrollXInner": "100%",
		   "bScrollInfinite": true,
		   "bScrollCollapse": true,
	       "sAjaxSource":'${ctx}/areaGroup/areagroupPage',
	       "fnServerData": loadData_unhandle,
	       "fnRowCallback":rowCallBack,
	       "iDisplayLength": 10, //每页数据
	       "bPaginate": true, //翻页功能
	       "bLengthChange": true, //改变每页显示数据数量
	       "bFilter": false, //过滤功能
	       "bSort": false, //排序功能
	       "bInfo": true,//页脚信息
	       "bAutoWidth": true,//自动宽度
	       "bStateSave": false, //状态保存，使用了翻页或者改变了每页显示数据数量，会保存在cookie中，下回访问时会显示上一次关闭页面时的内容
	       "bProcessing": true, //加载数据时显示正在加载信息
	       
	       "aoColumns": [  //排序控制
				null,
				{ "asSorting": [ "asc" ] },
				{ "asSorting": [ "desc", "asc", "asc" ] }
			],
	       
	       "sZeroRecords": "查询无记录！",
	       "oLanguage": { //汉化   
	           "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
	        },
	        "aoColumns": [ 
	                {"mData": null},
	                {"mData": 'groupName'}, 
	                {"mData": 'groupCode'},
	                {"mData": 'groupIntro'},
	                {"mData": 'areas'},
	                {"mData": 'warehouseType',
	                	 "render": function (obj) {
	                         return obj=='customer'?'商户':'能容';
	                      }
	                },
	                {"mData": 'customer',
	                	 "render": function (obj) {
	                         return obj==null?'':obj.customerName;
	                      }
	                },
	               	{"mData": null}
	         ],
	        "aoColumnDefs":[
		             	{"aTargets": [7],
		             		"render": function (obj,type,full) {
		             		  	var rurl = "";
		             			if(full.isSubWarehouse == 'Y' || full.warehouseType == 'customer'){
		             				rurl += "<a href=''#assignSubWarehouseDiv' onclick='showSubWarehouse(\""+full.groupCode+"\")' disabled=true role='button'  class='btn btn-primary' data-toggle='modal'>分配子仓</a>";
		             			}
		             			if(full.isSubWarehouse == 'N' && full.warehouseType == 'paiu'){
		             				rurl += "<a href='#assignSubWarehouseDiv' onclick='showSubWarehouse(\""+full.groupCode+"\")' role='button'  class='btn btn-primary' data-toggle='modal'>分配子仓</a>";
		             			}
		             			if(obj.warehouseType == 'paiu') {
		             				rurl+= "<a href='${ctx}/areaGroup/toCoverageArea?warehouseCode=" + obj.groupCode + "'  class='btn btn-info' >设置覆盖区</a>";		             			  
		             			}
		             			return rurl;
				           }
			            },
	       	   ]         	
	    });
	   return dataTables_areagroup;
	}
	
	//创建了行的回调 nRow为当前的tr
	function rowCallBack(nRow, aData, iDisplayIndex, iDisplayIndexFull){
		var iDisplayStart = dataTables_areagroup.fnSettings()._iDisplayStart;
		$("td:eq(0)", nRow).html(iDisplayStart+iDisplayIndex+1);
	}
	
	//回调函数_待处理订单
	function loadData_unhandle( sSource, aoData, fnCallback ) {
	 
	   aoData.push( { "name": "_EQ_customer.customerCode", "value": $("#_EQ_customerCode").val() } );
	   $.ajax({ 
	       "type": "post",
	       "contentType": "application/json", 
	       "url": sSource,    
	       "dataType": "json",   
	       "data": JSON.stringify(aoData),
	       "success": function(resp) {  
	       			fnCallback(resp);
	       			$("#total_unhandle").html("("+resp.iTotalRecords+")");
	       		}
	   });    
	}
	
	function showSubWarehouse(warehouseCode){
		openMainWarehouseCode = warehouseCode;
		
		var unAssignTempString = "";
		var assignTempString = "";
		$("#unAssignSubWarehouse").html(unAssignTempString);
		$("#assignSubWarehouse").html(assignTempString);
		$.ajax({
			type : 'post',
			url : '${ctx}/areaGroup/v2_querySubWarehouse',
			data : 'mainWarehouseCode=' + warehouseCode,
			dataType : 'json',
			success : function(data) {
				if (data.status == "success") {
					var allAreaGroup = data.allAreaGroup;
					var subAssign = data.subAssign;
					var flg = false;
					for(var i = 0 ; i<allAreaGroup.length;i++) {
						flg = false;
						for(var j = 0 ; j<subAssign.length;j++) {
							if(allAreaGroup[i].groupCode == subAssign[j].subWarehouseCode) {
								flg = true;
							}
						}	
						if(!flg){
							unAssignTempString += "<option value='"+allAreaGroup[i].groupCode+"'>"+allAreaGroup[i].groupName+"</option>";
						}
					}
					for(var j = 0 ; j<subAssign.length;j++) {
						assignTempString += "<option value='"+subAssign[j].subWarehouseCode+"'>"+subAssign[j].subWarehouseName+"</option>";
					}
					
					
					$("#unAssignSubWarehouse").html(unAssignTempString);
					$("#assignSubWarehouse").html(assignTempString);
					
				} else {
					jAlert("联系管理员！","温馨提示");
				}
			},

			error : function() {
				jAlert("联系管理员！","温馨提示");
			}
		});
	}
	var openMainWarehouseCode = "";
	function saveAssignSubWarehouse() {
		if(!openMainWarehouseCode) {
			jAlert("总仓编码丢失","温馨提示");
		}
		var assignSubWarehouseInfoStr = "";
		$('#assignSubWarehouse option').each(function(i,o){
			assignSubWarehouseInfoStr += $(o).val()+":"+(i+1)+",";
		});
		$.ajax({
			type : 'post',
			url : '${ctx}/areaGroup/v2_ajaxAssignSubWarehouse',
			data : 'mainWarehouseCode=' + openMainWarehouseCode+"&subWarehouseInofStr="+assignSubWarehouseInfoStr,
			dataType : 'json',
			success : function(data) {
				if (data.status == "success") {
					jAlert("保存成功！","温馨提示",function(){
						$("#queryForm").submit();
					});
				} else {
					jAlert("联系管理员！","温馨提示");
				}
			},
			error : function() {
				jAlert("联系管理员！","温馨提示");
			}
		});
	}
	
	function toAssign() {
		$('#unAssignSubWarehouse option:selected').each(function(i,o){
			if(openMainWarehouseCode && openMainWarehouseCode == $(o).val()){
				return;
			}
			$("#assignSubWarehouse").append($(o));
		});
	}
		
	function toUnAssign() {
		$('#assignSubWarehouse option:selected').each(function(i,o){
			$("#unAssignSubWarehouse").append($(o));
		});
	}
	function toUp() {
		//选中的索引,从0开始
		var optionIndex = $('#assignSubWarehouse').get(0).selectedIndex;
		//如果选中的不在最上面,表示可以移动
		if(optionIndex > 0){ 
			var selectList = $('#assignSubWarehouse option:selected');
			$('#assignSubWarehouse option:selected').each(function(i,o){
				$(o).insertBefore($(o).prev('option')); 
				
			});
			
		}
	}
	function toDown() {
		var optionIndex = $('#assignSubWarehouse').get(0).selectedIndex;
		var selectedLength = $('#assignSubWarehouse option:selected').length;
		//选中的索引,从0开始
		var optionLength = $('#assignSubWarehouse')[0].options.length; 
		//如果选中的不在最下面,表示可以移动
		if((optionIndex+selectedLength-1) < (optionLength-1)){ 
			var selectList = $('#assignSubWarehouse option:selected');
			var assList = $('#assignSubWarehouse option:selected');
			for(var i = assList.length - 1 ; i >= 0;i-- ){
				$(assList[i]).insertAfter($(assList[i]).next('option')); 
				
			}
		}
	}
	function toTop() {
		//选中的索引,从0开始
		var optionIndex = $('#assignSubWarehouse').get(0).selectedIndex;
		//如果选中的不在最上面,表示可以移动
		if(optionIndex > 0){
			var assList = $('#assignSubWarehouse option:selected');
			for(var i = 0 ; i <assList.length;i++ ){
				if(i==0){
					$(assList[i]).insertBefore( $('#assignSubWarehouse option').get(0) );
				}else {
					$(assList[i]).insertBefore( assList[i-1]); 
				}
				
			}
		}
	}
	function toBottom() {
		var optionIndex = $('#assignSubWarehouse').get(0).selectedIndex;
		var selectedLength = $('#assignSubWarehouse option:selected').length;
		//选中的索引,从0开始
		var optionLength = $('#assignSubWarehouse')[0].options.length; 
		//如果选中的不在最下面,表示可以移动
		if((optionIndex+selectedLength-1) < (optionLength-1)){ 
			var assList = $('#assignSubWarehouse option:selected');
			for(var i = 0 ; i <assList.length;i++ ){
				if(i==0){
					$(assList[i]).insertAfter( $('#assignSubWarehouse option').last() );
				}else {
					$(assList[i]).insertBefore( assList[i-1]); 
				}
				
			}
		}
	}
</script>
		<div class="row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
			<div class="box-header">
					<i class="fa fa-search"></i>
					<h3 class="box-title">搜索栏</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">	
				<form class="form-search" id="queryForm" >
						<div class="form-group row">
							<div class="col-sm-3">
								<div class="input-group col-sm-12">
									<label class="input-group-text">商家名称：</label>
									 <select name="_EQ_customerCode" class="form-control" id="_EQ_customerCode" value="${param._customerCode}">
										<option value=""></option>
										<c:forEach items="${customerList}" var="customer">
											<option value="${customer.customerCode}"
												<c:if test="${param._customerCode eq customer.customerCode}">selected=selected</c:if>>${customer.customerName}</option>
										</c:forEach>
									</select>
									</div>
									</div>
									<div class="col-sm-9">
									<button type="button" class="btn btn-info" onclick="query();" >搜索</button>
									</div>
								</div>
					</form>
	</div>
	</div>
	</div>
	</div>
	
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">查询结果</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead><tr>
			    <th>序号</th>
			    <th>分仓名称</th>
			    <th>分仓编码</th>
			    <th>分仓说明</th>
			    <th>区域</th>
			    <th>分仓类型</th>
			    <th>所属商家</th>
			    <th>操作</th>
			</tr></thead>
		<tbody>
		<%-- <c:forEach items="${groupList}" var="g" varStatus="i">
			<tr>
			   <td>${i.count}</td>
			   <td>${g.groupName}</td>
			   <td>${g.groupCode}</td>
			   <td>${g.groupIntro}</td>
			   <td  width="500px">
			   		<div style='width: 500px;display:block;word-break: break-all;word-wrap: break-word;'>
   						${g.areas}
   					</div>
			   	</td>
			   <td>
			     <c:if test="${g.warehouseType eq 'paiu'}">能容</c:if>
			     <c:if test="${g.warehouseType eq 'customer'}">商户</c:if>
			   </td>
			   <td>${g.customer.customerName}</td>
			    <td>
			    	<c:if test="${g.isSubWarehouse=='Y' || g.warehouseType eq 'customer'}">
						<a href="#assignSubWarehouseDiv" onclick="showSubWarehouse('${g.groupCode}')" disabled=true role="button"  class="btn btn-primary" data-toggle="modal">分配子仓</a>
			    	</c:if>
			    	
			    	<c:if test="${g.isSubWarehouse=='N' && g.warehouseType eq 'paiu'}">
				    	<a href="#assignSubWarehouseDiv" onclick="showSubWarehouse('${g.groupCode}')" role="button"  class="btn btn-primary" data-toggle="modal">分配子仓</a>
			    	</c:if>
			    </td>
			</tr>
		</c:forEach> --%>
		</tbody>
	</table>
	</div>
	</div>
	</div>
	</div>
	
	<div class="modal fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" style="width:1400;" id="assignSubWarehouseDiv">
			<div class="modal-dialog" >
			<div class="modal-content" >
			<div class="modal-header">
				<h3 id="myModalLabel">
					分配仓库
				</h3>
			</div>
			<div class="modal-body" style="width: 1300;height:300px; overflow: auto;">
				<table class="table table-striped table-bordered table-condensed"
					id="subWarehouseList">
					<thead>
						<tr>
							<th>未分配</th>
							<th></th>
							<th>已分配</th>
							<th>优先级</th>
						</tr>
					</thead>
					<tr>
						<td>
							<select id="unAssignSubWarehouse" size=12 style="width:210px;height:200px;"    class="oldSelectStyle" ondblclick="toAssign()" multiple="multiple"></select>
						</td>
						
						<td align="center" valign="middle">
							<br />
							<br />
							<br />
							<a onclick="toAssign()" role="button"  class="btn btn-primary" data-toggle="modal"> > </a>
							<br />
							<br />
							<a onclick="toUnAssign()" role="button"  class="btn btn-primary" data-toggle="modal"> < </a>
						</td>	
						
						<td>
							<select id="assignSubWarehouse" size=12  style="width:210px;height:200px;"    class="oldSelectStyle"  ondblclick="toUnAssign()"  multiple="multiple"></select>
						</td>
						<td>
						<br />
							<a onclick="toUp()" role="button"  class="btn btn-primary" data-toggle="modal"> 上移  </a>
							<br /><br />
							<a onclick="toDown()" role="button"  class="btn btn-primary" data-toggle="modal"> 下移 </a>
							<br /><br />
							<a onclick="toTop()" role="button"  class="btn btn-primary" data-toggle="modal"> 置顶 </a>
							<br /><br />
							<a onclick="toBottom()" role="button"  class="btn btn-primary" data-toggle="modal"> 置低 </a>
						</td>
					</tr>
				</table>
			</div>
			<div class="modal-footer">
				<a href="#" onclick="saveAssignSubWarehouse()" role="button"  class="btn btn-primary" data-toggle="modal">
					保存
				</a>
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		</div>
	</div>
		
		
</section>	
</body>
</html>
